package servlets;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jmysql.Cmysql;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.SimpleFileResolver;

/**
 * Servlet implementation class ReporteTransacciones
 */
@WebServlet("/ReporteTransacciones")
public class ReporteTransacciones extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReporteTransacciones() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//Tenemos 3 parametros
		
		
		String reporte=request.getParameter("ReporteJasper");
		String areaReporte=request.getParameter("areaReporte");
		System.out.println("-----:"+areaReporte+request.getParameterNames().toString());
		
//		String realpath=getServletContext().getRealPath("/Reportes/"+reporte+".jasper");
		String realpath=getServletContext().getRealPath("/Reportes/TSinFecha.jasper");
		String realpath_=getServletContext().getRealPath("/Reportes/");
		Connection conexion;
				
		 response.setHeader( "Content-Disposition", "attachment; filename=\"Reporte de transacciones.pdf\"" );
         response.setContentType("application/pdf");
         try{
        	 	conexion = (Connection) new Cmysql().dataSource.getConnection();
        	 
//              String realpath=getServletContext().getRealPath("/");
                File reportFile = new File(realpath);
                if (!reportFile.exists())
                         throw new JRRuntimeException("El archivo jasper no se encontro.");
                @SuppressWarnings("deprecation")
                JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
         
                Map<String,Object> parameters = new HashMap<String,Object>();
                 
             	parameters.put("p_area",new Integer(4));
//				parameters.put("imprimir_carga",(String) "1");
//				parameters.put("imprimir_descarga", (String) "1");
                 
                 
                parameters.put(JRParameter.REPORT_FILE_RESOLVER, new SimpleFileResolver(new File(realpath_)));
                 
                 
                 		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conexion);
                         JRPdfExporter exporter = new JRPdfExporter();
                         exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
                         
                         OutputStream ouputStream = response.getOutputStream();
                         exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
                         try {
                                 exporter.exportReport();
                         } 
                         catch (JRException e) {
                                 e.printStackTrace();
                         }  
                         finally
                         {
                                 if (ouputStream != null)
                                 {
                                         try
                                         {
                                                 ouputStream.close();
                                         }
                                         catch (IOException ex)
                                         {
                                                 ex.printStackTrace();
                                         }
                                 }
                         }
                  parameters.clear();
                  conexion.close();
         }
         catch (Throwable e)
         {
                 e.printStackTrace();
         }
		
	}

}
